package com.banach.prema.repository;

import com.banach.prema.dto.query.SosPageQuery;
import com.banach.prema.dto.vo.SosVO;
import com.banach.prema.repository.dataobject.SosDO;
import com.banach.prema.repository.mapper.SosMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ego.framework.core.tookit.BeanToolkit;
import com.ego.framework.model.CustomizePage;
import com.ego.framework.jdbc.repository.BaseRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class SosRepository extends BaseRepository<SosMapper, SosDO, SosVO> {

    public CustomizePage<SosVO> selectPage(SosPageQuery pageQuery){
        LambdaQueryWrapper<SosDO> queryWrapper = Wrappers.<SosDO>lambdaQuery()
                .eq(StringUtils.isNotBlank(pageQuery.getType()),SosDO::getType,pageQuery.getType())
                .like(StringUtils.isNotBlank(pageQuery.getUserName()),SosDO::getUserName,pageQuery.getUserName())
                .eq(pageQuery.getUserId() != null,SosDO::getUserId,pageQuery.getUserId())
                .ge(pageQuery.getBeginTime()  != null,SosDO::getCreateTime, pageQuery.getBeginTime())
                .le(pageQuery.getEndTime()  != null,SosDO::getCreateTime, pageQuery.getEndTime())
                .orderByAsc(SosDO::getUserId)
                .orderByDesc(SosDO::getMobile)
                .orderByDesc(SosDO::getCreateTime);
        IPage<SosDO> page = new Page(pageQuery.getPageNo(),pageQuery.getPageSize());
        page = this.baseMapper.selectPage(page, queryWrapper);
        CustomizePage<SosVO> customizePage = new CustomizePage(BeanToolkit.getInstance().copyList(page.getRecords(), SosVO.class), page.getSize(),page.getCurrent(), page.getTotal());
        return customizePage;
    }

}
